/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package central;

import java.rmi.RemoteException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;

/**
 *
 * @author Martin
 */
public class ControladorLlamadas {

    private static ControladorLlamadas instancia;

    private ControladorLlamadas() throws RemoteException {
    }

    public static ControladorLlamadas getInstance() throws RemoteException {
        if (instancia == null) {
            instancia = new ControladorLlamadas();
        }
        return instancia;

    }

    public Llamada obtenerLlamada(String extencion) {
        try {
            EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2databasePU");
            EntityManager em = factory.createEntityManager();
            Llamada llamada = (Llamada) em.createNamedQuery("Llamada.buscarPorExtencion").setParameter("extencion", extencion).getSingleResult();
            return llamada;
        } catch (NoResultException e) {
            Llamada l = new Llamada();
            l.setDuration("00:00'00'");
            return l;
        }
    }
    public void persistirPersona(Llamada llamada) {        
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("Central");
        EntityManager em = factory.createEntityManager();
        em.getTransaction().begin();
        em.persist(llamada);
        em.getTransaction().commit();
    }
}
